﻿using Common.Type;
using System;
using System.Configuration;
using System.Linq;

namespace Authentication.Core.Configuration
{
    public static class SystemConfiguration
    {
        private static string region;
        public static string Region
        {
            get
            {
                if (string.IsNullOrEmpty(region))
                {
                    region = string.Empty;
                }
                return region;
            }
            set
            {
                region = value;
            }
        }
        private static string connectionString;
        public static string ConnectionString
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(connectionString)) return connectionString;
                connectionString = ConfigurationManager.AppSettings["ConnectionString"];
                if (string.IsNullOrWhiteSpace(connectionString)) return "DefaultConnection";
                return connectionString;
            }
        }
        private static string schema;
        public static string Schema
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(schema)) return schema;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "SCHEMA");
                if (!string.IsNullOrEmpty(key)) schema = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(schema)) return "dbo";
                schema = schema.ToUpper();
                return schema;
            }
        }
        private static string userHOApprovedDao;
        public static string UserHOApprovedDao
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(userHOApprovedDao)) return userHOApprovedDao;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "USERHOAPPROVEDDAO");
                if (!string.IsNullOrEmpty(key)) userHOApprovedDao = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(userHOApprovedDao)) return "";
                //userHOApprovedDao = userHOApprovedDao.ToUpper();
                return userHOApprovedDao;
            }
        }
        private static string userBICApprovedDao;
        public static string UserBICApprovedDao
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(userBICApprovedDao)) return userBICApprovedDao;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "USERBICAPPROVEDDAO");
                if (!string.IsNullOrEmpty(key)) userBICApprovedDao = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(userBICApprovedDao)) return "";
                //userBICApprovedDao = userBICApprovedDao.ToUpper();
                return userBICApprovedDao;
            }
        }
        private static string userMBDApprovedDao;
        public static string UserMBDApprovedDao
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(userMBDApprovedDao)) return userMBDApprovedDao;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "USERMBDAPPROVEDDAO");
                if (!string.IsNullOrEmpty(key)) userMBDApprovedDao = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(userMBDApprovedDao)) return "";
                //userBICApprovedDao = userBICApprovedDao.ToUpper();
                return userMBDApprovedDao;
            }
        }
        private static string postionHeadBranch;
        public static string PostionHeadBranch
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(postionHeadBranch)) return postionHeadBranch;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "POSTIONHEADBRANCH");
                if (!string.IsNullOrEmpty(key)) postionHeadBranch = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(postionHeadBranch)) return "";
                //userBICApprovedDao = userBICApprovedDao.ToUpper();
                return postionHeadBranch;
            }
        }
        private static ConvertType? namingType;
        public static ConvertType NamingType
        {
            get
            {
                if (namingType.HasValue) return namingType.Value;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x == "NamingType");
                string stringVal = string.Empty;
                if (!string.IsNullOrEmpty(key)) stringVal = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(stringVal))
                {
                    namingType = ConvertType.CAPITALIZED;
                    return ConvertType.CAPITALIZED;
                }
                stringVal = stringVal.ToUpper();

                ConvertType result = ConvertType.CAPITALIZED;
                if (Enum.TryParse<ConvertType>(stringVal, out result))
                {
                    namingType = result;
                    return result;
                }

                return result;
            }
        }

        private static string tablePrefix;
        public static string TABLE_PREFIX
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(tablePrefix)) return tablePrefix;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "TABLE_PREFIX");
                if (!string.IsNullOrEmpty(key)) tablePrefix = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(tablePrefix)) return string.Empty;
                tablePrefix = tablePrefix.ToUpper();
                return tablePrefix;
            }
        }

    }
}
